package top.continew.minithreadpool.util;

import top.continew.minithreadpool.concurrent.ThreadPoolExecutor;

import java.util.concurrent.TimeUnit;

/**
 * 线程池工具类
 */
public class ThreadPoolUtils {
    public static void shutdownGracefully(ThreadPoolExecutor executor, long timeout, TimeUnit unit) {
        executor.shutdown();
        try {
            if (!executor.awaitTermination(timeout, unit)) {
                executor.shutdownNow();
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            executor.shutdownNow();
        }
    }

    public static void printThreadPoolStatus(ThreadPoolExecutor executor) {
        System.out.println("Thread Pool Status:");
        System.out.println("  Core Pool Size: " + executor.getCorePoolSize());
        System.out.println("  Max Pool Size: " + executor.getMaximumPoolSize());
        System.out.println("  Active Threads: " + executor.getActiveCount());
        System.out.println("  Pool Size: " + executor.getPoolSize());
        System.out.println("  Queue Size: " + executor.getQueue().size());
        System.out.println("  Completed Tasks: " + executor.getCompletedTaskCount());
    }
}
